<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
defined('RX_LINE_BREAK') or define('RX_LINE_BREAK', "\n");
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout=' ';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	/**
	 * 浏览器标题栏标题
	 * @var string
	 */
	private $_titleBarTitle=null;


	private $_pageSubTitle='';

	private $_pageOptionsHtml='';

	private $_pageOptions=[];

	/**
	 * 如果标题栏标题未设置，默认返回Untitled page字符串
	 * @return string
	 */
	public function getTitleBarTitle()
	{
		if(!$this->_titleBarTitle)
		{
			$this->_titleBarTitle = '系统管理';
		}
		return $this->_titleBarTitle;
	}

	public function setTitleBarTitle($title)
	{
		$this->_titleBarTitle = $title;
	}

	public function getPageSubTitle()
	{
		return $this->_pageSubTitle;
	}

	public function setPageSubTitle($title)
	{
		$this->_pageSubTitle = $title;
	}

	public function getPageOptionsHtml()
	{
		return join(RX_LINE_BREAK, $this->_pageOptions);
	}

	public function setPageOptionsHtml($html)
	{
		$this->_pageOptionsHtml = $html;
	}

	public function addPageOption($option)
	{
		$this->_pageOptions[] = $option;
	}

	public function actionIndex()
	{
		$this->forward(['admin']);
	}

	public function renderMenu($menuItems=null)
	{
		if(!$menuItems)
		{
			$menuItems = include Yii::app()->basePath.'/config/menu.php';
		}
// 		if(!Yii::app()->user->getIsAdmin())
// 		{
// 			$roles = Yii::app()->authManager->getAuthItems(2,Yii::app()->user->user_id);
// 			$keys = array_keys($roles);
// 			$role = $keys[0];
// 			foreach ($menuItems as $k=>$v)
// 			{
// 				if(isset($v['role']) && $v['role'] != $role)
// 					unset($menuItems[$k]);
// 			}
// 		}

		echo $this->renderMenuInternal($menuItems);
	}
	
	private function fnVarAtIndex($a,$k,$d=NULL) {

		return isset($a[$k])?$a[$k]:$d;
	}


	protected function renderMenuInternal($menuItems, &$hasAuthedItems=true)
	{
		$html = NULL;

		$hasAuthedItems = true;

		foreach($menuItems as $item)
		{
			
// 			$fnVarAtIndex = function(&$a, $k, $d=NULL) { return isset($a[$k])?$a[$k]:$d; };

			$url = $this->fnVarAtIndex($item, 'url');
			
// var_dump($url);die;
// 			if($url)
// 			{
// 				$accessNode = str_replace('/', '.', $url);
// 				if($accessNode[0]=='.')
// 					$accessNode = substr($accessNode, 1);

// 				if(Yii::app()->user->checkAccess($accessNode))
// 				{
// 					$hasAuthedItems = true;
// 				}
// 				else
// 				{
// 					$hasAuthedItems = false;
// 					continue;
// 				}
// 			}

			$children 	= $this->fnVarAtIndex($item, 'items');
			$icon 		= $this->fnVarAtIndex($item, 'icon');
			$badge 		= $this->fnVarAtIndex($item, 'badge');
			$badgeClass = $this->fnVarAtIndex($item, 'badgeClass', 'badge-info');
			$label		= $this->fnVarAtIndex($item, 'label');

			// 如果无已授权的子节点，则不渲染该节点，前提是当前节点存在子节点
			$hasAuthedItemsOfChildren = true;
			$childrenHtml = !$children
				?	''
				:	sprintf("<ul class='acc-menu'>%s</ul>", $this->renderMenuInternal($children, $hasAuthedItemsOfChildren));

			if(!$hasAuthedItemsOfChildren)
				continue;

			$html = sprintf("%s<li><a rel='%s' href='%s'>%s<span>%s</span>%s</a>%s</li>", $html,
				
				isset($item['url'])?$item['url']:"",
				// Redirect url
				!$url
					?	"javascript:;"
					:	Yii::app()->createUrl($url),

				// Item icon
				!$icon
					?	''
					:	sprintf("<i class='%s'></i>", $icon),

				// Item label
				$label,

				// Item badge
				!$badge
					?	''
					:	sprintf("<span class='badge %s'>%d</span>", $badgeClass, $badge),

				// Child items
				$childrenHtml

			);
		}
//         var_dump($html);die;
		return $html;
	}

// 	public function createUrl($route,$params=array(),$ampersand='&')
// 	{
// 		$params['token'] = isset($_GET['token']) ? $_GET['token'] : '';
// 		return parent::createUrl($route,$params,$ampersand);
// 	}

	
	//得到加密的值
	public static function getbase64_encode($str)
	{
		return str_ireplace('/', " ", base64_encode($str));
	}
	
	//得到解密值
	public static function getbase64_decode($str)
	{
		return base64_decode(str_ireplace(' ', "/",$str));
	}
	
	//后台上传图片
	public function adminupload(&$model,$img='Img',$filed='pic',$dir='other',$scenario='upload')
	{
		if(isset($_FILES[$img]) && !empty($_FILES[$img]['tmp_name'][$filed]) && is_file($_FILES[$img]['tmp_name'][$filed]))
		{
			$path = dirname(YII::app()->basePath)."/upload/{$dir}/";
			$file=CUploadedFile::getInstance($model,$filed);
			$my_path = $path.date('Ym/d');
	
			$model->scenario = $scenario;
	
			if(!is_dir($my_path))
			{
				mkdir($my_path,0777,true);
			}
	
			$deletepath = dirname(YII::app()->basePath);
			$savepath = $my_path.'/'.uniqid().'.'.$file->extensionName;
			$file->saveAs($savepath);
			//看是否有原来的图片
			$dele = dirname(YII::app()->basePath).$model->$filed;
				
			if(is_file($dele))
			{
				unlink($dele);
			}
				
			$model->$filed = str_replace($deletepath, '', $savepath);
	
		}
	}
}